/**
 * 
 */
package com.jm.web.builder.form;

import java.io.IOException;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.builder.BaseBuilder;
import com.jm.web.form.baseitem.FileItem;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 * Dec 22, 2011 10:28:18 AM
 */
public class FileBuilder extends BaseBuilder{
	protected FileItem file = null ;
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		file = new FileItem();
		file.InitTag(tag);
		file.setFromTag(true);//设置此控件是tag生成的
		file.init();
	}
	
	@Override
	public void RenderEnd() {
		String name = this.getBaseTag().GetExtValue("name", "");
		String id = this.getBaseTag().GetExtValue("id", name);
		
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("<field ");
		sb.Append(" name=\"%1$s\"",this.file.getName());
		if(this.getBaseTag().IsContainsKey("id"))
			sb.Append(" id=\"%1$s\"",this.file.getId());
		
		if(this.getBaseTag().IsContainsKey("captionWidth"))
			sb.Append(" captionWidth=\"%1$s\"",this.getBaseTag().GetExtValue("captionWidth", ""));
		if(this.getBaseTag().IsContainsKey("captionDisplay"))
			sb.Append(" captionDisplay=\"%1$s\"",this.getBaseTag().GetExtValue("captionDisplay", ""));
		if(this.getBaseTag().IsContainsKey("captionCss"))
			sb.Append(" captionCss=\"%1$s\"",this.getBaseTag().GetExtValue("captionCss", ""));
		if(this.getBaseTag().IsContainsKey("colspan"))
			sb.Append(" colspan=\"%1$s\"",this.getBaseTag().GetExtValue("colspan", ""));
		if(this.getBaseTag().IsContainsKey("caption"))
			sb.Append(" caption=\"%1$s\"",this.getBaseTag().GetExtValue("caption", ""));
		
		if(this.getBaseTag().IsContainsKey("rule"))
			sb.Append(" rule=\"%1$s\"",this.getBaseTag().GetExtValue("rule", ""));
		
		if(this.getBaseTag().IsContainsKey("caption"))
			sb.Append(" msg=\"%1$s\"",this.getBaseTag().GetExtValue("msg", ""));
		
		if(this.file.isRequired())
			sb.Append(" required=\"true\"");
		
		if(!StringHelper.IsNullOrEmpty(this.file.getRequiredMsg()))
			sb.Append(" requiredMsg=\"%1$s\"",this.file.getRequiredMsg());
		
		sb.Append(" >");
		
//		sb.Append("<div id=\"%1$s_div\"></div>",this.getBaseTag().GetExtValue("id", this.getBaseTag().GetExtValue("name", "")));
		
		
		int textWidth = Integer.parseInt(StringHelper.getValue(this.file.getWidth(),"100").replace("px",""));
		sb.Append("<div class=\"x-form-file-wrap\" style=\"width:%1$spx;left: 0;position: relative;top: 0;margin:0;padding:0;border:0;\">",textWidth);
		textWidth = textWidth - 22 ;
		
		sb.Append("\r\n<input type='text' ");
		sb.Append(" name=\"%1$s_path\" ",this.file.getName());
		sb.Append(" id=\"%1$s_path\" ",this.file.getId());
		sb.Append(" style=\"float: left;width:%1$spx;\" ",textWidth);
		sb.Append(" onblur=\"if($(this).val() == ''){$('#%1$s').val('')}\" ",this.file.getId());
		
		sb.Append(" />");
		
		sb.Append("<keep><div style=\"width:20px;_width:17px;margin-left:1px;padding:0 1px 0 1px;border: 1px #7F9DB9 solid !important;\" class=\"x-btn x-form-file-btn x-btn-default-small x-icon-text-left x-btn-icon-text-left x-btn-default-small-icon-text-left\">");
			sb.Append("<em class=\"\">");
				sb.Append("<button autocomplete=\"off\" role=\"button\" hidefocus=\"true\" type=\"button\"\" >");
					sb.Append("<span style=\"\" class=\"x-btn-inner upload-icon\">&nbsp;</span>");
				sb.Append("</button>");
			sb.Append("</em>");
			
			sb.Append("<input type=\"file\" style=\"float: left;padding:0;\" class=\"x-form-file-input\" ");
			sb.Append(" name=\"%1$s\" ",this.file.getName());
			sb.Append(" id=\"%1$s\" ",this.file.getId());
			sb.Append(" onchange=\"$('#%1$s_path').val($(this).val());\" ",this.file.getId());
			sb.Append(" />");
		sb.Append("</div></keep>");
		
		sb.Append("</div>");
		sb.Append("</field>");
		
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		

//		sb.Reset();
//		sb.Append("Ext.create('Ext.form.field.File', {");
//			sb.Append("renderTo: '%1$s_div',",id);
//			sb.Append("id: '%1$s',",id);
//			sb.Append("width: 300,");
////			sb.Append("style: 'height:17px;border: 1px #7F9DB9 solid !important;',");
//			sb.Append("emptyText: 'Select an file',");
//			sb.Append("fieldhidden:true,");
//			sb.Append("name: '%1$s',",name);
//			sb.Append("buttonText: '上傳',");
//			sb.Append("buttonConfig: {iconCls: 'upload-icon'}");
//		sb.Append("});");
//		this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,sb.toString());
	}
}
